Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
returning [object HTMLDivElement] when using document.getElementById

I am inserting this into my html div:

<script>
    document.getElementById("mArray").appendChild = '<%= mArray %>'
    console.log('<%= mArray %>')

  </script>
  <div id="mArray"></div>

then, inside my js file, I have this:

var mArray = document.getElementById("mArray")
console.log("33: " + mArray)

but when I log it, I get this:

33: [object HTMLDivElement]

i want it to return the array data, which should look like [1,3,5,7,9]

what am i doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When reading, use

var mArray = document.getElementById("mArray").textContent

and if you want to use it as a JS array (assuming it is a valid js array format), you could do

var mArrayContents = document.getElementById("mArray").textContent;
var myArray = JSON.parse( mArrayContents );

Additionally, when setting the value you do not have to use JS.
You can do it directly

<div id="mArray"><%= mArray %></div>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Right now you are logging the element itself, you need to check for the data inside. Depending on how the information is stored you could possibly get the information with something such as

document.getElementById("mArray").innerHTML;

or

document.getElementById("mArray").value;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda